<template>
  <el-dialog
    :title="title"
    v-bind="$attrs"
    @open="handlerOpen"
    v-on="$listeners"
  >
    <el-table
      :data="rigthsOfRole"
      style="width: 100%;margin-bottom: 20px;"
      row-key="id"
      border
      stripe
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
    >
      <el-table-column
        prop="id"
        label="id"
      />
      <el-table-column
        prop="authName"
        label="权限名称"
      />
      <el-table-column
        prop="path"
        label="路径"
      />
      <el-table-column>
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="danger"
            @click="deleteRightOfRole(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <span slot="footer">
      <el-button @click="closeDialog">返回</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { deelteRightOfRole, getRoles } from '@/api/rights'

export default {
  name: 'ROLEDETAIL',
  props: {
    role: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      rigthsOfRole: []
    }
  },
  computed: {
    title() {
      return this.role.roleName + '详情'
    }
  },
  methods: {
    closeDialog() {
      this.$emit('update:visible', false)
    },
    handlerOpen() {
      this.getTheRole()
    },
    /**
     * 接口不够用, 只能通过查找列表,来找到该role 的详情
     */
    async getTheRole() {
      const roles = await getRoles()
      const role = roles.find(i => i.id == this.role.id)

      this.rigthsOfRole = role.children || []
    },
    deleteRightOfRole(row) {
      this.$confirm(`删除 ${row.authName}, 是否继续?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        const data = {
          roleId: this.role.id,
          rightId: row.id
        }
        await deelteRightOfRole(data)
        this.$message({
          message: '删除成功',
          type: 'success'
        })
        this.getTheRole()
      }).catch(() => {
        console.log('取消删除')
      })
      console.log('row::', row)
    }
  }

}
</script>
